<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px;">
      <el-descriptions :column="5" border :colon="false" :label-style="{ width: '40px' }">
        <template #title>
          <div class="card-title"><span>入库任务单号 {{ detailInfo.inboundOrderCode }}</span></div>
        </template>
        <el-descriptions-item label-align="center">
          <template #label>
            <span class="card-content">任务类型</span>
          </template>
          <span class="card-content">
            <template v-for="(item, index) in inbound_type" :key="index">
              <div style="align-items: center;" v-if="Number(item.value) === detailInfo.type">{{ item.label }}</div>
            </template>
          </span>
        </el-descriptions-item>
        <el-descriptions-item label-align="center">
          <template #label>
            <span class="card-content">任务状态</span>
          </template>
          <span class="card-content">
            <template v-for="(item, index) in inbound_status" :key="index">
              <div style="align-items: center;" v-if="Number(item.value) === detailInfo.status">{{ item.label }}</div>
            </template> 
          </span>
        </el-descriptions-item>
        <el-descriptions-item label-align="center">
          <template #label><span class="card-content">开始时间</span></template><div style="align-items: center;" class="card-content">{{ parseTime(detailInfo.createTime) }}</div>
        </el-descriptions-item>
        <el-descriptions-item label-align="center">
          <template #label><span class="card-content">任务进度</span></template><div style="align-items: center;" class="card-content">{{ detailInfo.inboundProgress }}</div>
        </el-descriptions-item>
        <el-descriptions-item label-align="center">
          <template #label><span class="card-content">Port 口位置</span></template>
          <div style="align-items: center;" class="card-content">{{ detailInfo.port }}</div>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px" @submit.prevent>
      <!-- <el-form-item label="入库任务单号" prop="inboundOrderCode">
        <el-input
          v-model.trim="queryParams.inboundOrderCode"
          placeholder="请输入入库任务单号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item> -->
      <el-form-item label="物料码" prop="materialCode">
        <el-input
          v-model.trim="queryParams.materialCode"
          placeholder="请输入物料码"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model.trim="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="Reel ID" prop="reelId">
        <el-input
          v-model.trim="queryParams.reelId"
          placeholder="请输入 Reel ID"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="入库状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择入库状态"
          clearable
          style="width: 175px"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in inbound_detail_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:body:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:body:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:body:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:body:export']"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
        >下载导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <MyUpload
          buttonName="导入库单详情" 
          url="/api/inboundOrderBody/import"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
          />
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <TableScrollContainer :topHeight="360">
      <template #default="{ tableHeight }">
        <el-table v-loading="loading" :data="bodyList" @selection-change="handleSelectionChange" :height="tableHeight" border>
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="自增主键" align="center" prop="id" /> -->
          <el-table-column label="入库任务单号" align="center" prop="inboundOrderCode" />
          <el-table-column label="物料码" align="center" prop="materialCode" />
          <el-table-column label="批次号" align="center" prop="batchNo" />
          <el-table-column label="Reel ID" align="center" prop="reelId" />
          <el-table-column label="仓别" align="center" prop="warehouseType" />
          <el-table-column label="厂区" align="center" prop="plantCode" />
          <el-table-column label="入库状态" align="center" prop="status" >
            <template #default="scope">
              <dict-tag :options="inbound_detail_status" :value="scope.row.status"></dict-tag>
            </template>
          </el-table-column>
          
          <!-- <el-table-column label="完成时间" align="center" prop="finishTime" />
          <el-table-column label="更新时间" align="center" prop="updateTime" />                                                   -->
          <!-- <el-table-column label="物料类型" align="center" prop="materialType" /> -->
          <!-- <el-table-column label="数量" align="center" prop="materialQty" /> -->
          <!-- <el-table-column label="单位" align="center" prop="unit" /> -->
          <!-- <el-table-column label="料箱编号" align="center" prop="boxNo" />
          <el-table-column label="储格编号" align="center" prop="compartmentCode" />
          <el-table-column label="起点" align="center" prop="taskStart" />
          <el-table-column label="终点" align="center" prop="taskEnd" />
          <el-table-column label="AGV" align="center" prop="agvId" />
          <el-table-column label="AGV状态" align="center" prop="agvStatus" /> -->
          <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width"> -->
            <!-- <template #default="scope"> -->
              <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:body:edit']">修改</el-button> -->
              <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:body:remove']">删除</el-button> -->
            <!-- </template> -->
          <!-- </el-table-column> -->
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[10, 30, 50, 100]"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
    </TableScrollContainer>

    <!-- 添加或修改入库单体对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" :close-on-click-modal="false">
      <el-form ref="bodyRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="入库单号" prop="inboundOrderCode">
          <el-input v-model="form.inboundOrderCode" placeholder="请输入入库单号" />
        </el-form-item>
        <el-form-item label="物料号" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料号" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <el-form-item label="reelId" prop="reelId">
          <el-input v-model="form.reelId" placeholder="请输入reelId" />
        </el-form-item>
        <el-form-item label="物料数量" prop="materialQty">
          <el-input v-model="form.materialQty" placeholder="请输入物料数量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="InboundDetail">
import { parseTime } from '@/utils/ruoyi'
import { listBody, getBody, delBody, addBody, updateBody, getInboundInfoAPI } from "@/api/work/inboundDetail"
import TableScrollContainer from '@/components/Pagetable/TableScrollContainer.vue'

const { proxy } = getCurrentInstance()
const route = useRoute()
const { inbound_type, inbound_status,inbound_detail_status } = proxy.useDict('inbound_type', 'inbound_status','inbound_detail_status')

const bodyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const detailInfo = reactive({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inboundOrderCode: route.query.inboundOrderCode || null,
    materialCode: null,
    batchNo: null,
    reelId: null,
    materialQty: null,
    warehouseType: null,
    plantCode: null,
    status: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询入库单体列表 */
function getList() {
  loading.value = true
  listBody(queryParams.value).then(res => {
    if (res.errCode === '1') {
      bodyList.value = res.data.page.records
      total.value = res.data.page.total
      loading.value = false
    }
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inboundOrderCode: null,
    materialCode: null,
    batchNo: null,
    reelId: null,
    materialQty: null,
    warehouseType: null,
    plantCode: null,
    status: null,
    finishTime: null,
    updateTime: null
  }
  proxy.resetForm("bodyRef")
}

const getInboundTypeLabel = (value) => {
  const targetValue = Number(value)
  const matched = inbound_type._object.inbound_type.find(item => Number(item.value) === targetValue)
  // console.log(matched)
  return matched.label
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  console.log(queryParams.value)
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加入库单体"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBody(_id).then(res => {
    form.value = res.data.待替换
    open.value = true
    title.value = "修改入库单体"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bodyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBody(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      } else {
        addBody(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除入库单体编号为"' + _ids + '"的数据项？').then(function() {
    return delBody(_ids)
  }).then(res => {
    if (res.errCode === '1') {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    } else {
      proxy.$modal.msgError(res.msg)
    }
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('api/inboundOrderBody/export', {
    ...queryParams.value
  }, `入库单详情数据_${new Date().getTime()}.xlsx`)
}

const getFatherInfo = async() => {
  const res = await getInboundInfoAPI(route.query.inboundOrderCode)
  Object.assign(detailInfo, res.data.detail.header)
  // console.log(detailInfo)
}

// 下载模板
const downloadTemplate = async() => {
  proxy.download('/api/inboundOrderBody/downloadTemplate', {}, `入库单详情模板_${new Date().getTime()}.xlsx`)
}
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList()
}
// 部分成功回调
const handleUploadWarning = (res) => {
  getList()
}

const goToWork = () => {
  proxy.$router.push(`/inboundManagement/stockUp?inboundOrderCode=${route.query.inboundOrderCode}`)
}


getFatherInfo()
getList()
</script>

<style lang="scss" scoped>
:deep(.el-descriptions__title) {
  margin-bottom: 8px !important;
  font-size: large;
}

:deep(.el-descriptions__body) {
  background-color: transparent;
}

:deep(.el-descriptions__table) {
  .el-descriptions__cell {
    padding: 8px 12px !important;
  }
  
  .el-descriptions__label {
    font-weight: normal;
    color: #606266;
  }
}

.card-content {
  font-size: medium;
  // color: #ffffff;
  font-weight: bold;
  text-align: center; 
}
</style>
